import Vue from "vue"
import Vuex from "vuex"
import Storage from "@/util/storage.js"
Vue.use(Vuex)
const userInfo = Storage.getUse()||{};
const query = Storage.getQuery()||{};
const addr = Storage.getAddr()||{}
export default new Vuex.Store({
	state:{
		userInfo:userInfo,
		orderDetail:null,
		device:{},
		time:{
			day:0,
			hour:0,
			minute:0, 
			seconde:0
		},
		addressObj:addr,
		query:query,
		redPack:200000
	},
	mutations:{
		setTime(state,data){
			state.time = data
		},
		setOrderDetail(state,data){
			Storage.setOrderDetail(data)
			state.orderDetail = data;
		},
		setQuery(state,data){
			state.query = data;
			Storage.setQuery(data)
		},
		setAddress(state,data){
			Storage.setAddr(data)
			state.addressObj = data;			
		},
		setDevice(state,data){
			state.device = data
		},
		setUserInfo(state,data){
			Storage.setUse(data)
			state.userInfo = data
		}
	},
	actions:{
		getTime(state){
			var date = new Date();
			var y = date.getFullYear();
			var m = date.getMonth();
			var d = date.getDate() + 1;
			var time = new Date(y, m, d, 0, 0, 0);
			var num = time.getTime() - date.getTime();		
			var day = parseInt(num / (24 * 60 * 60 * 1000));
			num = num % (24 * 60 * 60 * 1000);
			var hour = parseInt(num / (60 * 60 * 1000));
			num = num % (60 * 60 * 1000);
			var minute = parseInt(num / (60 * 1000));
			num = num % (60 * 1000);
			var seconde = parseInt(num / 1000);
			// hour = hour < 10 ? '0' + hour : hour;
			// minute = minute < 10 ? '0' + minute : minute;
			// seconde = seconde < 10 ? '0' + seconde : seconde;
			// var timeStr = hour + ":" + minute + ":" + seconde;
			state.commit('setTime',{
				day,
				hour,
				minute,
				seconde
			})
		}
	},
	getters:{
		click_id(state){
			let click_id = click_id||"";
			switch (state.query.from){
				case 'mammut':
						if(state.query.click_id)click_id = state.query.click_id;
					break;
				case 'toutiao':
						if(state.query.clickid)click_id = state.query.clickid;
					break;
				case 'qtt':
						if(state.query.callback_url)click_id = state.query.callback_url;
					break;
				case 'jiguang':
						if(state.query.jcid)click_id = state.query.jcid;
					break;
				case 'tuia':
						if(state.query.a_tuiaId)click_id = state.query.a_tuiaId;
					break;
				case 'cat':
						if(state.query.bxmid)click_id = state.query.bxmid;
					break;
				case 'guangqu':
						if(state.query.ccbid)click_id = state.query.ccbid;
					break;
				case 'huawei':
				case 'way':
						if(state.query.callback)click_id=encodeURIComponent(state.query.callback);
					break;
				default:
						if(state.query.gdt_vid)click_id = state.query.gdt_vid;
					break;
			}
			Storage.setClkId(click_id)
			return click_id
		},
		isWX(state){
			let {ua} = state.device;
			let isWXWork = ua.toLowerCase().match(/wxwork/i) == 'wxwork'
			let isWeixin = !isWXWork && ua.toLowerCase().match(/micromessenger/i) == 'micromessenger';
			return isWeixin
		},
		getOrderDetail(state){
			if(state.orderDetail){
				return state.orderDetail
			}else{
				return Storage.getOrderDetail();
			}
		},
		getOrderList(state){
			return Storage.getOrderList();
		}
	}
})